In [1]:
def times_tables():
    lst = []
    for i in range(10):
        for j in range (10):
            lst.append(i*j)
    return lst

In [2]:
times_tables() == [i*j for i in range(10) for j in range(10)]


Out[2]:
True

In [4]:
# getting all the possible combinations e.g, aa01, i.e two letters then 2 nos
lowercase = 'abcdefghijklmnopqrstuvwxyz'
digits = '0123456789'

answer = [a+b+c+d for a in lowercase for b in lowercase 
         for c in digits for d in digits]

In [6]:
answer[:6]


Out[6]:
['aa00', 'aa01', 'aa02', 'aa03', 'aa04', 'aa05']

Numpy


In [7]:
import numpy as np

In [11]:
np.diag([2,3])


Out[11]:
array([[2, 0],
       [0, 3]])

In [12]:
np.array([1,2,3]*3)


Out[12]:
array([1, 2, 3, 1, 2, 3, 1, 2, 3])

In [13]:
np.repeat([1,2,3],2)


Out[13]:
array([1, 1, 2, 2, 3, 3])

In [15]:
p = np.ones([2,3],int)
p


Out[15]:
array([[1, 1, 1],
       [1, 1, 1]])

In [16]:
np.vstack([p, 2*p])


Out[16]:
array([[1, 1, 1],
       [1, 1, 1],
       [2, 2, 2],
       [2, 2, 2]])

In [17]:
np.hstack([p, 2*p])


Out[17]:
array([[1, 1, 1, 2, 2, 2],
       [1, 1, 1, 2, 2, 2]])

Operations


In [19]:
p.dtype


Out[19]:
dtype('int32')

In [20]:
p.T


Out[20]:
array([[1, 1],
       [1, 1],
       [1, 1]])

In [21]:
p.T.shape


Out[21]:
(3, 2)

In [24]:
p.astype('f')


Out[24]:
array([[ 1.,  1.,  1.],
       [ 1.,  1.,  1.]], dtype=float32)

In [25]:
a = np.array([-4,-2,1,3])

In [26]:
a.sum()


Out[26]:
-2

In [28]:
a.argmax() # index of the maximum value


Out[28]:
3

In [29]:
a.argmin()


Out[29]:
0

In [33]:
s = np.arange(13)**2
s


Out[33]:
array([  0,   1,   4,   9,  16,  25,  36,  49,  64,  81, 100, 121, 144])

In [34]:
s[0], s[4], s[0:3]


Out[34]:
(0, 16, array([0, 1, 4]))

In [35]:
s[-4:]


Out[35]:
array([ 81, 100, 121, 144])

In [37]:
s[-5::-1]


Out[37]:
array([64, 49, 36, 25, 16,  9,  4,  1,  0])

In [38]:
r = np.arange(36)
r.resize(6,6)
r


Out[38]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

In [40]:
r[2,3]


Out[40]:
15

In [41]:
r[3, 3:6]


Out[41]:
array([21, 22, 23])

In [42]:
r[:2, :-1]


Out[42]:
array([[ 0,  1,  2,  3,  4],
       [ 6,  7,  8,  9, 10]])

In [43]:
r[-1, ::2]


Out[43]:
array([30, 32, 34])

In [44]:
r[r > 30]


Out[44]:
array([31, 32, 33, 34, 35])

In [46]:
r[r>30] = 30
r


Out[46]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 30, 30, 30, 30, 30]])

Note


In [48]:
r2 = r[:3,:3]
r2


Out[48]:
array([[ 0,  1,  2],
       [ 6,  7,  8],
       [12, 13, 14]])

In [50]:
r2[:] = 0
r2


Out[50]:
array([[0, 0, 0],
       [0, 0, 0],
       [0, 0, 0]])

In [51]:
r # the elements in r are affected by the operation performed on r2


Out[51]:
array([[ 0,  0,  0,  3,  4,  5],
       [ 0,  0,  0,  9, 10, 11],
       [ 0,  0,  0, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 30, 30, 30, 30, 30]])

In [56]:
r_copy = r.copy()
r_copy


Out[56]:
array([[ 0,  0,  0,  3,  4,  5],
       [ 0,  0,  0,  9, 10, 11],
       [ 0,  0,  0, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 30, 30, 30, 30, 30]])

In [57]:
r_copy[:] = 10
r_copy


Out[57]:
array([[10, 10, 10, 10, 10, 10],
       [10, 10, 10, 10, 10, 10],
       [10, 10, 10, 10, 10, 10],
       [10, 10, 10, 10, 10, 10],
       [10, 10, 10, 10, 10, 10],
       [10, 10, 10, 10, 10, 10]])

In [58]:
r


Out[58]:
array([[ 0,  0,  0,  3,  4,  5],
       [ 0,  0,  0,  9, 10, 11],
       [ 0,  0,  0, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 30, 30, 30, 30, 30]])

Iterating over arrays


In [59]:
test = np.random.randint(0,10, (4,3))
test


Out[59]:
array([[0, 9, 3],
       [8, 4, 8],
       [1, 1, 9],
       [3, 4, 4]])

In [60]:
for row in test:
    print(row)


[0 9 3]
[8 4 8]
[1 1 9]
[3 4 4]

In [61]:
for i in range(len(test)):
    print(test[i])


[0 9 3]
[8 4 8]
[1 1 9]
[3 4 4]

In [62]:
for i, row in enumerate(test):
    print('row', i, 'is', row)


row 0 is [0 9 3]
row 1 is [8 4 8]
row 2 is [1 1 9]
row 3 is [3 4 4]

In [63]:
test2 = test**2
test2


Out[63]:
array([[ 0, 81,  9],
       [64, 16, 64],
       [ 1,  1, 81],
       [ 9, 16, 16]])

In [64]:
for i,j in zip(test, test2):
    print(i, '+', j, '=', i+j)


[0 9 3] + [ 0 81  9] = [ 0 90 12]
[8 4 8] + [64 16 64] = [72 20 72]
[1 1 9] + [ 1  1 81] = [ 2  2 90]
[3 4 4] + [ 9 16 16] = [12 20 20]

In [65]:
type(lambda x: x+1)


Out[65]:
function

Exercise


In [67]:
m = np.arange(36)
m.resize(6,6)
m


Out[67]:
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17],
       [18, 19, 20, 21, 22, 23],
       [24, 25, 26, 27, 28, 29],
       [30, 31, 32, 33, 34, 35]])

In [75]:
m.reshape(36)[::7]


Out[75]:
array([ 0,  7, 14, 21, 28, 35])

In [76]:
m[2:4,2:4]


Out[76]:
array([[14, 15],
       [20, 21]])

In [ ]: